package com.kcsj.controller.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kcsj.domain.Order;
import com.kcsj.service.OrderService;
import com.kcsj.util.Condition;
import com.kcsj.util.ControllerHelper;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

/**
 * @author 傅立贤
 * 25th Nov. 2020
 */
@Slf4j
@WebServlet("/order.ctl")
public class OrderController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //根据request对象，获得代表参数的JSON字串
        String req_json = JSONUtil.getJSON(request);
        if(req_json.equals("")){
            JSONObject message = MessageHelper.addMessage("请填写订单信息！");
            response.getWriter().println(message);
            return;
        }
        try {
            Order order = JSON.parseObject(req_json,Order.class);
            boolean add = OrderService.getInstance().add(order);
            if(add){
                JSONObject message = MessageHelper.addMessage("下单成功！");
                response.getWriter().println(message);
            }else {
                JSONObject message = MessageHelper.addMessage("下单失败！");
                response.getWriter().println(message);
            }
        }catch (SQLException e){
            e.printStackTrace();
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch(Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //根据request对象，获得代表参数的JSON字串
        String req_json = JSONUtil.getJSON(request);
        try{
            Order order = JSON.parseObject(req_json, Order.class);
            OrderService.getInstance().update(order);
            JSONObject message = MessageHelper.addMessage("修改成功");
            response.getWriter().println(message);
        }catch (SQLException e){
            e.printStackTrace();
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
            log.error(e.getMessage());
        }catch (Exception e){
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取前端请求的信息
        String orderNo = request.getParameter("orderNo");
        String condition = request.getParameter("condition");
        try {
            if (orderNo != null) {
               this.responseOrder(response,orderNo);
            } else if (condition != null) {
                JSONObject jsonObject = JSONObject.parseObject(condition);
                List<Condition> conditionList = ControllerHelper.getConditions(jsonObject);
                this.responseOrders(response,conditionList);
            } else {
                this.responseOrders(response,null);
            }
        }
            catch(SQLException e){
                JSONObject message = MessageHelper.dateBaseError();
                //响应message到前端
                response.getWriter().println(message);
                e.printStackTrace();
            }catch(Exception e){
                 JSONObject message = MessageHelper.netWordError();
            //响应message到前端
                response.getWriter().println(message);
                e.printStackTrace();
            }
    }
    private void responseOrder(HttpServletResponse response,String orderNo)
            throws SQLException, IOException {
        //根据订单号查找订单
        Order order = OrderService.getInstance().find(orderNo);
        String order_json = JSON.toJSONString(order);
        JSONObject message = MessageHelper.addData(order_json);
        //响应message到前端
        response.getWriter().println(message);
    }

    //按查询条件响应订单对象
    private void responseOrders(HttpServletResponse response,List<Condition> conditionList)
            throws SQLException, IOException {
        //获得所有学生
        Collection<Order> orders = OrderService.getInstance().findAll(conditionList);
        String orders_json = JSON.toJSONString(orders, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject message = MessageHelper.addData(orders_json);
        //响应message到前端
        response.getWriter().println(message);
    }
}
